/** 
 * 1、样式编译
 * 2、脚本编译
 * 3、页面模板编译
 * 4、图片和字体文件的转化
 * 5、其他文件及文件清除
 * 6、自动载入插件
 * 7、热更新开发服务器 browser-sync
 * 8、监视变化以及构建过程优化
 * 9、useref文件引用处理（例如第三方引用包）+ 一些压缩操作
 * 10、重新规划构建
 * 11、补充（构建任务完成后规整导出任务）
*/
const { src, dest, parallel, series, watch } = require('gulp')
const del = require('del') // 清除文件

const browserSync = require('browser-sync')

const loadPlugins = require('gulp-load-plugins') // 自动加载插件
const plugins = loadPlugins()
const bs = browserSync.create()

// const plugins.sass = require('gulp-sass') // 转化sass编译插件
// const plugins.babel = require('gulp-babel') // 转化es6编译插件  需要同步安装 @babel/core @babel/preset-env
// const plugins.swig = require('gulp-swig') // 转化html模板编译插件
// const plugins.imagemin = require('gulp-imagemin') // 压缩图片


const data = {
  menus: [],
  pkg: require('./package.json'),
  date: new Date()

}
const clean = () => {
  return del('dist', 'temp')
}


// 定义style私有任务
const style = () => {
  return src('src/assets/styles/*.scss', { base: 'src' })
    .pipe(plugins.sass({ outputStyle: 'expanded' })) // 有下划线名称的文件不会被转化  会被认为是项目依赖的文件
    .pipe(dest('temp'))
    .pipe(bs.reload({stream: true}))

}

const script = () => {
  return src('src/assets/scripts/*.js', { base: 'src'})
    .pipe(plugins.babel({ presets: ['@babel/preset-env']})) // @babel/preset-env 必须要配置，不然不会转化编译
    .pipe(dest('temp'))
    .pipe(bs.reload({stream: true}))


}

const page = () => {
  return src('src/*.html', { base: 'src' })
    .pipe(plugins.swig({ data }))
    .pipe(dest('temp'))
    .pipe(bs.reload({stream: true}))

}

const image = () => {
  return src('src/assets/images/**', { base: 'src' })
    .pipe(plugins.imagemin())
    .pipe(dest('dist'))
}

const font = () => {
  return src('src/assets/fonts/**', { base: 'src' })
  .pipe(plugins.imagemin())
  .pipe(dest('dist'))
}

const extra = () => {
  return src('public/**', { base: 'public' })
  .pipe(dest('dist'))
}

const serve = () => {
  watch('src/assets/styles/*.scss', style)
  watch('src/assets/scripts/*.js', script)
  watch('src/*.html', page)
  // watch('src/assets/images/**', image)
  // watch('src/assets/fonts/**', font)
  // watch('public/**', extra)
  watch([
    'src/assets/images/**',
    'src/assets/fonts/**',
    'public/**'
  ], bs.reload)

  bs.init({
    notify: false,
    port: 2080,
    // open: false,
    // files: 'dist/**',
    server: {
      baseDir: ['temp', 'src', 'public'],
      routes: {
        '/node_modules': 'node_modules'
      }
    }
  })
}

const useref = () => {
  return src('temp/*.html', { base: 'temp'})
    .pipe(plugins.useref({ searchPath: ['temp', '.'] }))
    // 压缩Html、css、JavaScript
    .pipe(plugins.if(/\.js$/, plugins.uglify()))
    .pipe(plugins.if(/\.css$/, plugins.cleanCss()))
    .pipe(plugins.if(/\.html$/, plugins.htmlmin({ 
      collapseWhitespace: true,  // html收起标签
      minifyCSS: true,  // html中css处理
      minifyJS: true  // html中js处理
    })))
    .pipe(dest('dist'))
}






const compile = parallel(style, script, page)
const build = series(
  clean, 
  parallel(
    series(compile, useref), 
    image, 
    font, 
    extra
    )
  ) 
const develop = series(compile, serve)

module.exports = {
  clean,
  build,
  develop
}
